//
//  HomeViewModel.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/6.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import AFNetworking

class HomeViewModel: NSObject {

    var list: [PersonalBlogViewModel] = [PersonalBlogViewModel]()
    
    
    func getData(since_id: Int, max_id: Int,success: (count: Int) -> (), failure: (NSError) -> ()) {
        
        let manager = AFHTTPSessionManager()
        
        let parameters = [
            
            "access_token": OauthViewModel.sharedInstance.access_token,
            "since_id": since_id,
            "max_id": max_id
        ]
        
        manager.GET("https://api.weibo.com/2/statuses/home_timeline.json", parameters: parameters, progress: nil, success: { (_, responser) -> Void in
            
            //                        print(responser)
            if let dict = responser as? [String: AnyObject] {
                
                let statuses = dict["statuses"] as! [[String: AnyObject]]
                
                var temp: [PersonalBlogViewModel] = [PersonalBlogViewModel]()
                
                for statuse in statuses {
                    
                    let statuseModel = JSONModel(dict: statuse)

                    temp.append(PersonalBlogViewModel(model: statuseModel))
                }
                
                if since_id > 0 {
                    self.list = temp + self.list
                }else {
                    self.list = self.list + temp
                }
                
                success(count: temp.count)
            }
            
            }) { (_, error) -> Void in
//                print(error)
                failure(error) 
        }
    }
    
}
